﻿Imports System
Imports System.Data

Imports Moca.Db

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports Moca.Db.Helper

Namespace Db.Helper

	'''<summary>
	'''OleDbSQLAccessHelperTest のテスト クラスです。すべての
	'''OleDbSQLAccessHelperTest 単体テストをここに含めます
	'''</summary>
	<TestClass()> _
	Public Class OleDbSQLAccessHelperTest
		Inherits Moca.Test.AbstractUnitTest

		Private testContextInstance As TestContext

		Protected dbaTest As IDaoOleDbTest

		'''<summary>
		'''現在のテストの実行についての情報および機能を
		'''提供するテスト コンテキストを取得または設定します。
		'''</summary>
		Public Property TestContext() As TestContext
			Get
				Return testContextInstance
			End Get
			Set(value As TestContext)
				testContextInstance = value
			End Set
		End Property

#Region "追加のテスト属性"
		'
		'テストを作成するときに、次の追加属性を使用することができます:
		'
		'クラスの最初のテストを実行する前にコードを実行するには、ClassInitialize を使用
		<ClassInitialize()> _
		Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
			ClassInitialize(testContext)
		End Sub

		'クラスのすべてのテストを実行した後にコードを実行するには、ClassCleanup を使用
		<ClassCleanup()> _
		Public Shared Sub MyClassCleanup()
			ClassCleanup()
		End Sub

		''各テストを実行する前にコードを実行するには、TestInitialize を使用
		'<TestInitialize()> _
		'Public Sub MyTestInitialize()
		'	Me.inject()
		'End Sub

		''各テストを実行した後にコードを実行するには、TestCleanup を使用
		'<TestCleanup()> _
		'Public Sub MyTestCleanup()
		'End Sub

#End Region

		'''<summary>
		'''ConnectionTest のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ConnectionTest()
			Dim target As OleDbSQLAccessHelper = dbaTest.GetHelper
			Dim actual As DataTable
			actual = target.ConnectionTest
		End Sub

		'''<summary>
		'''CDbParameterName のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub CDbParameterNameTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim name As String = "column_name"
			Dim expected As String = "column_name"
			Dim actual As String
			actual = target.CDbParameterName(name)
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''ErrorCount のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ErrorCountTest()
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				Dim target As IDbAccessHelper = dbaTest.GetHelper
				Dim expected As Integer = 2
				Dim actual As Integer
				actual = target.ErrorCount(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Assert.AreEqual(expected, actual)
			End Try
		End Sub

		'''<summary>
		'''ErrorNumbers のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ErrorNumbersTest()
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				Dim target As IDbAccessHelper = dbaTest.GetHelper
				Dim expected() As String = {OleDbErrorNumbers.TableCompatibilityRestrictions, OleDbErrorNumbers.StatementEnd, OleDbErrorNumbers.DuplicationPKey}
				Dim actual() As String
				actual = target.ErrorNumbers(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Assert.AreEqual(expected.Count, actual.Count)
				Assert.AreEqual(expected(0), actual(0))
				Assert.AreEqual(expected(1), actual(1))
				Assert.AreEqual(expected(2), actual(2))
			End Try
		End Sub

		'''<summary>
		'''GetSchemaColumns のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaColumnsTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim table As DbInfoTable = New DbInfoTable("MocaDemo", "dbo", "tbDemoEntities", "TABLE")
			Dim actual As DbInfoColumnCollection

			actual = target.GetSchemaColumns(table)
			Assert.AreEqual(4, actual.Count)
			Assert.AreEqual(5, actual("ID").MaxLength)
			Assert.AreEqual(0, actual("ID").Precision)
			Assert.AreEqual(0, actual("ID").Scale)
			Assert.AreEqual(True, actual("ID").UniCode)
			Assert.AreEqual(10, actual("Code").MaxLength)
			Assert.AreEqual(10, actual("Code").Precision)
			Assert.AreEqual(0, actual("Code").Scale)
			Assert.AreEqual(False, actual("Code").UniCode)
			Assert.AreEqual(256, actual("Name").MaxLength)
			Assert.AreEqual(0, actual("Name").Precision)
			Assert.AreEqual(0, actual("Name").Scale)
			Assert.AreEqual(True, actual("Name").UniCode)
			Assert.AreEqual(1073741823, actual("Note").MaxLength) ' MAX
			Assert.AreEqual(0, actual("Note").Precision)
			Assert.AreEqual(0, actual("Note").Scale)
			Assert.AreEqual(True, actual("Note").UniCode)
		End Sub

		'''<summary>
		'''GetSchemaFunctions のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaFunctionsTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim name As String = "MocaDemo.dbo.3.funcDemo"
			Dim actual As DbInfoFunctionCollection
			actual = target.GetSchemaFunctions
			Assert.AreEqual(1, actual.Count)
			Assert.AreEqual(True, actual.Keys.Contains(name))
		End Sub

		'''<summary>
		'''GetSchemaProcedures のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaProceduresTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim name As String = "MocaDemo.dbo.3.usrDemo"
			Dim actual As DbInfoProcedureCollection
			actual = target.GetSchemaProcedures
			Assert.AreEqual(1, actual.Count)
			Assert.AreEqual(True, actual.Keys.Contains(name))
		End Sub

		'''<summary>
		'''GetSchemaTable のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaTableTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim tablename As String = "tbDemoEntities"
			Dim actual As DbInfoTable
			actual = target.GetSchemaTable(tablename)
			Assert.AreEqual("MocaDemo", actual.Catalog)
			Assert.AreEqual("dbo", actual.Schema)
			Assert.AreEqual(tablename, actual.Name)
			Assert.AreEqual("TABLE", actual.Typ)
		End Sub

		'''<summary>
		'''GetSchemaTables のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaTablesTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim tablename1 As String = "MocaDemo.dbo.TABLE.tbDemoEntities"
			Dim tablename2 As String = "MocaDemo.dbo.SYSTEM TABLE.__MigrationHistory"
			Dim obj As Object = Nothing
			Dim actual As DbInfoTableCollection
			actual = target.GetSchemaTables
			Assert.AreEqual(2, actual.Count)
			Assert.AreEqual(True, actual.Keys.Contains(tablename1))
			Assert.AreEqual(True, actual.Keys.Contains(tablename2))
		End Sub

		'''<summary>
		'''HasSqlNativeError のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				actual = target.HasSqlNativeError(ex.GetBaseException, OleDbErrorNumbers.DuplicationPKey)
				Debug.Print(ex.GetBaseException.Message)
				Debug.Print(String.Join(",", target.ErrorNumbers(ex.GetBaseException)))
			End Try
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''HasSqlNativeErrorDuplicationPKey のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorDuplicationPKeyTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				actual = target.HasSqlNativeErrorDuplicationPKey(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Debug.Print(String.Join(",", target.ErrorNumbers(ex.GetBaseException)))
			End Try
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''HasSqlNativeErrorTimtout のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorTimtoutTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Dim actualNo As String = String.Empty
			Try
				dbaTest.TimeoutTest(10, 2)
			Catch ex As Exception
				actual = target.HasSqlNativeErrorTimtout(ex.GetBaseException)
				actualNo = target.ErrorNumbers(ex.GetBaseException)(0)
				Debug.Print(ex.GetBaseException.Message)
				Debug.Print(String.Join(",", target.ErrorNumbers(ex.GetBaseException)))
			End Try
			Assert.AreEqual(expected, actual)
			Assert.AreEqual(CStr(OleDbErrorNumbers.TimeOut), actualNo)
		End Sub

		'''<summary>
		'''RefreshProcedureParameters のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub RefreshProcedureParametersTest()
			Const C_SQL As String = "usrDemo"
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim cmd As IDbCommand = dbaTest.GetMe.CreateCommandStoredProcedure(C_SQL).Command
			Dim pram As IDataParameter
			target.RefreshProcedureParameters(cmd)
			Assert.AreEqual(3, cmd.Parameters.Count)
			pram = CType(cmd.Parameters(0), IDataParameter)
			Assert.AreEqual("RETURN_VALUE", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.ReturnValue, pram.Direction)
			pram = CType(cmd.Parameters(1), IDataParameter)
			Assert.AreEqual("P1", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.Input, pram.Direction)
			pram = CType(cmd.Parameters(2), IDataParameter)
			Assert.AreEqual("P2Out", pram.ParameterName)
			Assert.AreEqual(ParameterDirection.InputOutput, pram.Direction)
		End Sub

		'''<summary>
		'''PlaceholderMark のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub PlaceholderMarkTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As String = "?"
			Dim actual As String
			actual = target.PlaceholderMark
			Assert.AreEqual(expected, actual)
		End Sub

	End Class

End Namespace
